<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/api.css" />
    <link rel="stylesheet" type="text/css" href="../css/aui.css" />
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
    <style>
        .hide {
            display: none;
        }

        .c1 {
            position: fixed;
            top: 0;
            bottom: 0;
            left: 0;
            right: 0;
            background: rgba(0, 0, 0, .5);
            z-index: 2;
        }

        .c2 {
            background-color: white;
            position: fixed;
            width: 400px;
            height: 300px;
            top: 50%;
            left: 50%;
            z-index: 3;
            margin-top: -150px;
            margin-left: -200px;
            padding-top: 2%;
            padding-left: 2%;
        }

        .c3 {
            background-color: white;
            position: fixed;
            width: 400px;
            height: 300px;
            top: 50%;
            left: 50%;
            z-index: 3;
            margin-top: -150px;
            margin-left: -200px;
            padding-top: 2%;
            padding-left: 2%;
        }

        #modal p {
            margin-left: 80px;
        }

        input::placeholder {
            color: #D0D0D0;
        }
    </style>
</head>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #03a9f4;position:fixed;" id="header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            更换零件
        </div>
    </header>
    <div class="aui-content aui-margin-b-15" style="margin-top:60px;">

        <div id="citemList" class="aui-content">
            <div class="aui-form" id="citemForm">

            </div>
            <ul class="aui-list aui-form-list">
                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label">源关键件码</div>
                        <div class="aui-list-item-input">
                            <input id="kapartCodeY"></input>
                        </div>
                    </div>
                </li>
                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label" style="width: 60%;">替换关键件码</div>
                        <div class="aui-list-item-input">
                            <input id="kapartCodeT"></input>
                        </div>
                    </div>
                </li>
        </div>

    </div>
    <div class=' aui-btn aui-btn-info aui-btn-block' onclick='doSave()'>更换零件
    </div>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript" src="../script/hammer.min.js"></script>
<script type="text/javascript">


    var barcode = "";
    //初始化
    apiready = function () {

        $("#kapartCodeY").focus();

        barcode = api.pageParam.barcode;

    };

    //产品条码监听回车事件
    $("#kapartCodeY").on('keypress', function (e) {
        var keycode = e.keyCode;
        var tm = $(this).val();
        if (keycode == '13') {
            doConfirmY()
        }
    });

    //产品条码监听回车事件
    $("#kapartCodeT").on('keypress', function (e) {
        var keycode = e.keyCode;
        var tm = $(this).val();
        if (keycode == '13') {
            doSave()
        }
    });

    //保存
    function doConfirmY() {

        var kapartCodeY = document.getElementById('kapartCodeY').value;

        if (kapartCodeY == null || kapartCodeY == "") {

            api.toast({
                msg: "请先扫源关键件码！",
                duration: 3000
            });
            return
        }

        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/doConfirmY";
        var data = {
            values: {
                kapartCodeY: kapartCodeY,
                barcode: barcode,
                un: un,
            }
        };

        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                console.log(ret);
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                if (myobj.code == 0) {
                    $("#kapartCodeT").focus();
                } else {
                    api.toast({
                        msg: myobj.msg,
                        duration: 3000
                    });
                }
            }
        });


    }


    //保存
    function doSave() {

        var kapartCodeY = document.getElementById('kapartCodeY').value;

        if (kapartCodeY == null || kapartCodeY == "") {

            api.toast({
                msg: "请先扫源关键件码！",
                duration: 3000
            });
            return
        }

        var kapartCodeT = document.getElementById('kapartCodeT').value;


        if (kapartCodeT == null || kapartCodeT == "") {

            api.toast({
                msg: "请先扫码替换关键件码！",
                duration: 3000
            });
            return
        }
        if(kapartCodeY===kapartCodeT){
            api.toast({
                msg: "源关键件码不能与替换关键件码一样！",
                duration: 3000
            });
            $("#kapartCodeT").val("");
            $("#kapartCodeT").focus();
            return
        }

        api.confirm({
            title: '更换',
            msg: '确认更换该关键件条码？',
            buttons: ['确定', '取消']
        }, function (ret, err) {
            var index = ret.buttonIndex;
            if (index == 1) {
                var un = localStorage.getItem('un');
                var serverUrl = localStorage.getItem('serverUrl');
                var url = serverUrl + "/app/replaceMrl";
                var data = {
                    values: {
                        kapartCodeT: kapartCodeT,
                        barcode: barcode,
                        kapartCodeY: kapartCodeY,
                         un: un
                    }
                };

                api.ajax({
                    url: url,
                    method: 'post',
                    dataType: 'json',
                    timeout: 30,
                    returnAll: false,
                    data: data
                }, function (ret, err) {
                    if (ret) {
                        console.log(ret);
                        var data = JSON.stringify(ret);
                        var myobj = eval("(" + data + ")");

                        if (myobj.code == 0) {
                            alert("保存成功!")
                            api.closeToWin({ name: api.pageParam.name });

                        } else {
                            api.toast({
                                msg: myobj.msg,
                                duration: 3000
                            });
                        }
                    }
                });

            } else {
                $("#kapartCodeY").focus();
            }
        });

    }

    //返回按钮
    function closeWin() {
        // 
        if (api.pageParam.barcode == undefined) {
            api.closeWin({});
        } else {
            api.closeToWin({ name: api.pageParam.name });
            // api.execScript({
            //     name: api.pageParam.name,
            //     script: 'refreshData()'
            // });
        }
    }


</script>

</html>